package cn.sinata.wanglin.neighbor.activity;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilada.xldutils.activitys.CropImageActivity;
import com.xilada.xldutils.activitys.SelectPhotoDialog;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.BitmapUtils;
import com.xilada.xldutils.utils.FileUtils;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.xldUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Group;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.ResultData;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class CreateGroupChatStepThreeActivity extends TitleActivity {

    private String userId;
    private String housingId;
    private String name;
    private String labels;
//    private String url;
    private String info;
    @BindView(R.id.image)
    SimpleDraweeView imageView;
    @BindView(R.id.tv_content)
    EditText tv_content;
    private String filePath ;

    @Override
    protected int setContentId() {
        return R.layout.activity_create_group_chat_step_three;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("添加群封面");
        addRightButton("完成", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(filePath)) {
                    showToast("请选择一张封面图片");
                    return;
                }
                info = tv_content.getText().toString().trim();
                if (TextUtils.isEmpty(info)) {
                    showToast("请输入群介绍吧");
                    return;
                }
                showDialog();
                createGroup();
            }
        });

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        housingId = SharedPreferencesUtils.getString(Constant.SP_HOUSING_ID);
        name = getIntent().getStringExtra("name");
        labels = getIntent().getStringExtra("labels");

    }

    void createGroup(){
        housingId = MyApplication.houseid;
//        url = "http://img3.qianzhan123.com/news/201612/19/20161219-bffea87cdb4728ea_600x5000.jpg";
        Observable.just(filePath)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String s) {
                        String path = BitmapUtils.compressImagePath(s);
                        return Observable.just(new File(path));
                    }
                })
                .flatMap(new Func1<File, Observable<String>>() {
                    @Override
                    public Observable<String> call(File file) {
                        return HttpManager.uploadFile(mContext,file);
                    }
                })
                .flatMap(new Func1<String, Observable<ResultData<Group>>>() {
                    @Override
                    public Observable<ResultData<Group>> call(String s) {
                        return HttpManager.createGroup(userId, housingId, name, labels, s, info);
                    }
                }).subscribe(new ResultDataSubscriber<Group>(this) {
                    @Override
                    public void onSuccess(String msg, Group group) {

                        setResult(RESULT_OK);
                        finish();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0) {
                if (data != null) {
                    String filePath = data.getStringExtra(SelectPhotoDialog.DATA);
//                    imageView.setImageURI(Uri.parse("file://"+filePath));
                    cropImage(filePath);
                }
            } else if (requestCode == 2) {
                if (data != null) {
                    filePath = data.getStringExtra("path");
                    imageView.setImageURI(Uri.parse("file://"+filePath));
                }
            }
        }
    }

    void cropImage(String path){
        ActivityUtil.create(mContext)
                .go(CropImageActivity.class)
                .put("uri",path)
                .put("scale",1.478f)
                .startForResult(2);
    }

    @OnClick({R.id.tv_select_img})
    void onViewClick(View v){
        switch (v.getId()) {
            case R.id.tv_select_img:
                ActivityUtil.create(this)
                        .go(SelectPhotoDialog.class)
                        .startForResult(0);
                break;
        }
    }

    @Override
    public void finish() {
        if (!TextUtils.isEmpty(xldUtils.PICDIR)) {
            FileUtils.deleteFiles(new File(xldUtils.PICDIR));
        }
        super.finish();
    }
}
